<template>
    <div>
        <common-head title="编辑联系人"/>
        <van-address-edit
        :area-list="areaList"
        show-postal
        show-delete
        :address-info = 'contact'
        show-set-default
        show-search-result
        :search-result="searchResult"
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
        @delete="onDelete"
        />
    </div>
</template>

<script>
import CommonHead from '@components/CommonHead'
import { areaList } from '@vant/area-data'
import { Toast } from 'vant'
export default {
  methods: {
    fetchContact () {
      this.contact = this.$store.state.contact.contacts[parseInt(this.$route.query.index)]
    },
    onSave (contact) {
      this.$store.commit('contact/update_contact', {
        index: parseInt(this.$route.query.index),
        contact
      })
      Toast('修改成功')
      this.$router.replace('/contactLists')
    },
    onDelete (contact) {
      this.$store.commit('contact/delete_contact', {
        index: parseInt(this.$route.query.index)
      })
      Toast('删除成功')
      this.$router.replace('/contactLists')
    }
  },
  created () {
    this.fetchContact()
  },
  data () {
    return {
      areaList,
      contact: {} // 表示当前编辑的联系人
    }
  },
  components: {
    CommonHead
  }
}
</script>

<style lang="scss" scoped>
.van-address-edit{
    padding-top: 50px;
}
</style>
